package bugtracking

/**
 * Created by IntelliJ IDEA.
 * User: alexandrua
 * Date: 6/8/11
 * Time: 2:51 PM
 * To change this template use File | Settings | File Templates.
 */
class Bug {
    int bug_id
    Date creation_ts
    Date delta_ts
    String short_desc
    String op_sys
    String priority
    String rep_platform
    String bug_status
    String resolution
    String severity
    Actor reporter
    Actor assigned_to
    String product
    String component
    String bug_version
    String target_milestone
    Actor qa_contact

    static belongsTo = Actor
    static hasMany = [history: History]

    static constraints = {
        assigned_to(nullable: true)
        reporter(nullable: true)
        qa_contact(nullable: true)
//        history(nullable: true)
    }

    public Bug mergeBug() {
        Bug temp = Bug.findByBug_id(bug_id)
        if (temp != null) {
            //TODO uncomment bellow if
//            if (temp.delta_ts != delta_ts) {
                //the bugs will be extracted for last date but synchronization will be done more often so a bug once modified will be extracted from mozilla more times
                temp.delta_ts = delta_ts;
                temp.short_desc = short_desc;
                temp.op_sys = op_sys;
                temp.priority = priority;
                temp.rep_platform = rep_platform;
                temp.bug_status = bug_status;
                temp.resolution = resolution;
                temp.severity = severity;
                temp.reporter = reporter.mergeActor();
                temp.assigned_to = assigned_to.mergeActor();
                temp.product = product;
                temp.component = component;
                temp.bug_version = bug_version;
                temp.target_milestone = target_milestone;
                temp.qa_contact = temp.qa_contact.mergeActor();
                history.eachWithIndex {History h, int index ->
//                    if(History.findByBug_whenAndWho(h.bug_when, Actor.findByEmail(h.who.email))==null){
                    if(History.findByBug_when(h.bug_when)==null){
//                        h.bug = temp;
//                        temp.addToHistory(h.mergeHistory())
                        temp.addToHistory(h.mergeHistory());
                    }
                }
//            }
                 temp.save();
        } else {
            reporter = reporter.mergeActor();
            assigned_to = assigned_to.mergeActor();
            qa_contact = qa_contact.mergeActor();
            this.save();
            history.each {History h ->
                h.bug = this;
                h = h.mergeHistory()
            }
            temp = this;
        }
        return temp;
    }
}
